﻿@inject IDataCacheService _dataCacheService
@inject IEventService _eventService
@inject ILive2DService _live2DService
@inject NavigationManager NavigationManager

@implements IDisposable


<CnGalWebSite.Components.Containers.MiniModeContainer>
    @if (isReady && !Hide)
    {
        @if (_dataCacheService.ThemeSetting.HideKanban || _dataCacheService.IsApp)
        {
            <CnGalWebSite.Shared.MasaComponent.Shared.Tips.FloatToolTip @ref="FloatTool" />
        }
        else
        {
            <AuthorizeView Roles="Tester">
                <Authorized>
                    <ErrorHandler IsSSR="ToolHelper.IsSSR" ShowDetailedErrorInfor="_dataCacheService.ThemeSetting.ShowDetailedErrorInfor">

                        <CnGalWebSite.Kanban.KanbanLive2D>
                            <CnGalWebSite.Kanban.Components.Buttons.ExternalButton @ref="ExternalButton" />
                        </CnGalWebSite.Kanban.KanbanLive2D>
                    </ErrorHandler>
                </Authorized>
                <NotAuthorized>
                    <CnGalWebSite.Shared.MasaComponent.Shared.Tips.FloatToolTip @ref="FloatTool" />
                </NotAuthorized>
            </AuthorizeView>
        }
    }


</CnGalWebSite.Components.Containers.MiniModeContainer>

@code {
    bool isReady;
    bool Hide;

    CnGalWebSite.Shared.MasaComponent.Shared.Tips.FloatToolTip FloatTool;
    CnGalWebSite.Kanban.Components.Buttons.ExternalButton ExternalButton;

    protected override void OnInitialized()
    {
        _eventService.KanbanChanged -= OnKanbanChanged;
        _eventService.KanbanChanged += OnKanbanChanged;

        NavigationManager.LocationChanged -= LocationChanged;
        NavigationManager.LocationChanged += LocationChanged;

        LocationChanged(null, null);
    }

    public void LocationChanged(object sender, LocationChangedEventArgs args)
    {
        var hide = NavigationManager.Uri.Contains("almanac");
        if(hide!=Hide)
        {
            Hide=hide;
            StateHasChanged();
        }
    }

    public void DisplayArticleCatalogButton(ArticleViewModel model)
    {
        if (FloatTool != null)
        {
            FloatTool?.DisplayArticleCatalogButton(model);
        }
        else if (ExternalButton != null)
        {
            ExternalButton?.DisplayArticleCatalogButton(model);
        }

    }

    public async void OnKanbanChanged()
    {
        isReady = true;
        await InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        NavigationManager.LocationChanged -= LocationChanged;
        _eventService.KanbanChanged -= OnKanbanChanged;
        GC.SuppressFinalize(this);
    }

}
